/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.model.endereco;

import br.com.bld.util.facade.AbstractDao;
import br.com.bld.util.facade.EntityFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;

/**
 *
 * @author Pedro
 */
class EnderecoDao extends AbstractDao {

    public EnderecoDao(Connection conn, EntityFactory entityFactory) {
        super(conn, entityFactory);
    }

    private EnderecoPK getPk(ResultSet rs) throws SQLException {

        int i = 0;

        EnderecoPK resposta = new EnderecoPK(rs.getLong(++i), rs.getLong(++i));

        return resposta;
    }

    private EnderecoEntity getEntity(ResultSet rs) throws SQLException {
        EnderecoEntity resposta = new EnderecoEntity();

        int i = 2;

        resposta.setLogradouro(rs.getString(++i));
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setComplemento(rs.getString(i));
        }
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setCodgIbge(rs.getInt(i));
        }
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setNumero(rs.getString(i));
        }
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setCep(rs.getInt(i));
        }
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setBairro(rs.getString(i));
        }

        return resposta;
    }

    public int insert(EnderecoEntity entity, EnderecoPK pk) throws SQLException {

        String sql = createInsert();

        PreparedStatement ps = null;

        try {
            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());
            ps.setString(++i, entity.getLogradouro());
            if (entity.getComplemento() != null && !entity.getComplemento().equals("")) {
                ps.setString(++i, entity.getComplemento());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getCodgIbge() != null && entity.getCodgIbge() > 0) {
                ps.setInt(++i, entity.getCodgIbge());
            } else {
                ps.setNull(++i, Types.NULL);
            }
            if (entity.getNumero() != null && !entity.getNumero().equals("")) {
                ps.setString(++i, entity.getNumero());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getCep() != null && entity.getCep() > 0) {
                ps.setLong(++i, entity.getCep());
            } else {
                ps.setNull(++i, Types.NUMERIC);
            }
            if (entity.getBairro() != null && !entity.getBairro().equals("")) {
                ps.setString(++i, entity.getBairro());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }

            return ps.executeUpdate();
        } finally {
            close(ps);
        }
    }

    public int update(EnderecoEntity entity, EnderecoPK pk) throws SQLException {

        String sql = createUpdate();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setString(++i, entity.getLogradouro());
            if (entity.getComplemento() != null && !entity.getComplemento().equals("")) {
                ps.setString(++i, entity.getComplemento());
            } else {
                ps.setNull(+i, Types.VARCHAR);
            }
            if (entity.getCodgIbge() != null && entity.getCodgIbge() > 0) {
                ps.setInt(++i, entity.getCodgIbge());
            } else {
                ps.setNull(++i, Types.NULL);
            }
            if (entity.getNumero() != null && !entity.getNumero().equals("")) {
                ps.setString(++i, entity.getNumero());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getCep() != null && entity.getCep() > 0) {
                ps.setLong(++i, entity.getCep());
            } else {
                ps.setNull(++i, Types.NUMERIC);
            }
            if (entity.getBairro() != null && !entity.getBairro().equals("")) {
                ps.setString(++i, entity.getBairro());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public int delete(EnderecoPK pk) throws SQLException {
        String sql = createDelete();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public HashMap<EnderecoPK, EnderecoEntity> selectAll() throws SQLException {

        HashMap<EnderecoPK, EnderecoEntity> resposta = new HashMap();
        String sql = createSelect();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public EnderecoEntity selectByPk(EnderecoPK pk) throws SQLException {

        EnderecoEntity resposta = null;

        String sql = createSelectByPk();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public HashMap<EnderecoPK, EnderecoEntity> selectByPage(Integer numrPagina, Integer qtdePorPagina) throws SQLException {

        HashMap<EnderecoPK, EnderecoEntity> resposta = new HashMap();
        String sql = createSelectByPage(numrPagina, qtdePorPagina);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }
}
